<?php 
// +----------------------------------------------------------------------
// | RXThinkCMF敏捷开发框架 [ 赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2023 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | Licensed LGPL-3.0 RXThinkCMF并不是自由软件，未经许可禁止去掉相关版权
// +----------------------------------------------------------------------
// | 官方网站: https://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: @牧羊人 团队荣誉出品
// +----------------------------------------------------------------------
// | 版权和免责声明:
// | 本团队对该软件框架产品拥有知识产权（包括但不限于商标权、专利权、著作权、商业秘密等）
// | 均受到相关法律法规的保护，任何个人、组织和单位不得在未经本团队书面授权的情况下对所授权
// | 软件框架产品本身申请相关的知识产权，禁止用于任何违法、侵害他人合法权益等恶意的行为，禁
// | 止用于任何违反我国法律法规的一切项目研发，任何个人、组织和单位用于项目研发而产生的任何
// | 意外、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、
// | 附带或衍生的损失等)，本团队不承担任何法律责任，本软件框架禁止任何单位和个人、组织用于
// | 任何违法、侵害他人合法利益等恶意的行为，如有发现违规、违法的犯罪行为，本团队将无条件配
// | 合公安机关调查取证同时保留一切以法律手段起诉的权利，本软件框架只能用于公司和个人内部的
// | 法律所允许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\admin\service\ExperienceScanService;

use app\admin\model\Experience;
use app\admin\model\Project;
use app\admin\model\ExperienceScan as ExperienceScanModel;
use app\admin\model\School;

/**
 * 体验扫码-控制器
 * @author 牧羊人
 * @since: 2024/07/28
 * Class Experience
 * @package app\admin\controller
 */
class Experiencescan extends Backend
{
    /**
     * 初始化方法
     * @author 牧羊人
     * @since: 2024/07/28
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->service = new ExperienceScanService();
    }
    
    // 老师端-体验-统计
    public function statistics()
    {
         // 参数
        $param = request()->param();
        
        // 实例化模型
        $experienceScan = new ExperienceScanModel();
        
        $map = [];
        
       // 学校名称
       $name = isset($param['school_name']) ? trim($param['school_name']) : '';
       if ($name) {
          $map[] = ['school_name', 'like', "%{$name}%"];
       }
       
       
       // 体验ID
       $experienceId = isset($param['experience_id']) ? $param['experience_id'] : '';
       if ($experienceId) {
          $map[] = ['experience_id', '=', $experienceId];
       }else{
          // 最新的体验
          $experience = Experience::order('sort', 'desc')->find();
          $map[] = ['experience_id', '=', $experience['id']]; 
       }
       $map[] = ['mark', '=', 1];
         
       // 分组并统计每组的用户数
       $groupedSchool = $experienceScan->field('experience_id,school_id,school_name,COUNT(DISTINCT project_id) as project_count, COUNT(scan_time) as scan_sum')
                              ->where($map)
                              ->group('school_id')
                              ->select();
                              
         foreach ($groupedSchool as $value) {
              // code...
              $value['experience_name'] = Experience::where('id',$value['experience_id'])->value('name');
        }                      
        
        $result = [
                'code' => 0, 
                'msg' => 'ok',
                'data' => $groupedSchool
            ];
        
        return $this->jsonReturn($result);
        
        
    }
    
      // 项目体验-统计详情
    public function detail()
    {
         // 参数
        $param = request()->param();
        
        // 实例化模型
        $experienceScan = new ExperienceScanModel();
        
        $map = [];
        
       // 学校名称
       $name = isset($param['school_name']) ? trim($param['school_name']) : '';
       if ($name) {
          $map[] = ['school_name', 'like', "%{$name}%"];
       }
       
       // 学校ID
       $school_id = isset($param['school_id']) ? $param['school_id'] : 0;
       if ($school_id) {
          $map[] = ['school_id', '=', $school_id];
       }
       
       // 项目ID
       $project_id = isset($param['project_id']) ? $param['project_id'] : 0;
       if ($project_id) {
          $map[] = ['project_id', '=', $project_id];
       }
       
       // 体验ID
       $experienceId = isset($param['experience_id']) ? $param['experience_id'] : '';
       if ($experienceId) {
          $map[] = ['experience_id', '=', $experienceId];
       }else{
          // 最新的体验
          $experience = Experience::order('sort', 'desc')->find();
          $map[] = ['experience_id', '=', $experience['id']]; 
       }
         
       // 分组并统计每组的用户数
       $groupedSchool = $experienceScan->field('school_id,school_name,project_id, COUNT(scan_time) as scan_sum')
                              ->where($map)
                              ->group('project_id')
                              ->select();
                              
        foreach ($groupedSchool as $value) {
              // code...
              $value['project_name'] = Project::where('id',$value['project_id'])->value('name');
        }
        
        $result = [
                'code' => 0, 
                'msg' => 'ok',
                'data' => $groupedSchool
            ];
        
        return $this->jsonReturn($result);
        
        
    }
	
                            	                        
}